package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"gorm.io/gorm"
	"strconv"
	"yk/models"
)

type DrugController struct {
	beego.Controller
}

func (c *DrugController) Get() {

}
func (c *DrugController) Add() {
	name := c.GetString("name")
	sex := c.GetString("sex")
	tel := c.GetString("tel")
	docker := c.GetString("docker")
	time := c.GetString("time")
	num, _ := c.GetInt("num")
	zt := c.GetString("zt")
	sm := c.GetString("sm")

	drug := models.Drug{
		Name:   name,
		Sex:    sex,
		Tel:    tel,
		Docker: docker,
		Time:   time,
		Num:    num,
		Zt:     zt,
		Sm:     sm,
	}

	err := models.DB.Create(&drug).Error
	if name == "" || sex == "" || tel == "" || docker == "" || time == "" || num == 0 || zt == "" || sm == "" {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "所有信息不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: drug,
		}
		c.ServeJSON()
	}
}
func (c *DrugController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)
	pageData := make(map[string]interface{})
	key := "drug-list" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var count int64
		offest := (page - 1) * size
		query := models.DB.Model(&models.Drug{})
		query.Count(&count)
		name := c.GetString("name")
		if name != "" {
			query = query.Where("name like?", "%"+name+"%")
		}
		var drug []models.Drug
		err = query.Offset(offest).Limit(size).Find(&drug).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {

			pageData["count"] = count
			pageData["drugs"] = drug
			marshal, _ := json.Marshal(&pageData)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功-mysql",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}

}
func (c *DrugController) Info() {
	id, _ := c.GetInt("id")
	var drug models.Drug
	key := "drug-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	xx := "drug-rank"
	if err != nil {
		err = models.DB.Where("id=?", id).First(&drug).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "详情展示失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			models.Rdb.ZIncrBy(xx, 1, drug.Name)
			marshal, _ := json.Marshal(&drug)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "详情展示成功-mysql",
				Data: drug,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &drug)
		models.Rdb.ZIncrBy(xx, 1, drug.Name)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "详情展示成功-redis",
			Data: drug,
		}
		c.ServeJSON()
	}

}
func (c *DrugController) Update() {
	id, _ := c.GetInt("id")
	zt := c.GetString("zt")
	drug := models.Drug{
		Model: gorm.Model{ID: uint(id)},
		Zt:    zt,
	}
	err := models.DB.Where("id=?", id).Updates(&drug).Error
	//if zt == "未进行" {
	//	c.Data["json"] = JsonR{
	//		Code: 0,
	//		Msg:  "进行中的和已完成的无法更改为未进行状态",
	//		Data: nil,
	//	}
	//	c.ServeJSON()
	//	return
	//}
	//if zt == "进行中" || zt == "已完成" {
	//	c.Data["json"] = JsonR{
	//		Code: 0,
	//		Msg:  "未进行的无法直接更改为已完成",
	//		Data: nil,
	//	}
	//	c.ServeJSON()
	//	return
	//}
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "修改成功",
			Data: drug,
		}
		c.ServeJSON()
	}
}
func (c *DrugController) Rank() {
	key := "drug-rank"
	models.Rdb.ZCard(key).Val()
	ranks, err := models.Rdb.ZRevRangeWithScores(key, 0, 7-1).Result()
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "排行榜展示失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "排行榜展示成功",
			Data: ranks,
		}
		c.ServeJSON()
	}
}
